<html>

 <head><title>CS 564 PHP Project Search Result Page</title></head>

 <?php echo "<a href=\"https://cs564.cs.wisc.edu/laska/advanced_search.html\">Back to advance search</a>\n"?><p>

 <?php echo "<a href=\"https://cs564.cs.wisc.edu/laska/index.html\">Back to main page</a>\n"?><p>

 <body>
   <tr>
     <td colspan="2" align="center" valign="top">
      Here are the search results (searched by title):<br>
       <table border="1" width="75%">
        <tr>
         <td align="center" bgcolor="#cccccc"><b>Name</b></td>
         <td align="center" bgcolor="#cccccc"><b>Type</b></td>
         <td align="center" bgcolor="#cccccc"><b>Rarity</b></td>
        </tr>	
 <?php

  $query = "SELECT * FROM magic_schema.cards where ";//name='".$name."'";
  $lastSet = False;

  // First check the itemid to see if it has been set
  if (strlen($_POST['name'])) {
    $name = $_POST['name'];
    $query .= "name='".$name."'";
    $lastSet = True;
  }

  if (strlen($_POST['type'])) {
    if ($lastSet) {
      $query .= " and ";
    }

    // Format type
    $type = $_POST['type'];
    $type = strtolower($type);
    if ($type == 'creature' || $type == 'c') {
      $type = 'c';
    }
    else if ($type == 'land' || $type == 'l') {
      $type = 'l';
    }
    else if ($type == 'spell' || $type == 's') {
      $type = 's';
    }

    $query .= "type='".$type."'";
    $lastSet = True;
  }

  if (strlen($_POST['rarity'])) {
    if ($lastSet) {
      $query .= " and ";
    }

    // Format rarity
    $rarity = $_POST['rarity'];
    $rarity = strtolower($rarity);
    if ($rarity == 'common' || $rarity == 'c') {
      $rarity = 'c';
    }
    else if ($rarity == 'uncommon' || $rarity == 'u') {
      $rarity = 'u';
    }
    else if ($rarity == 'rare' || $rarity == 'r') {
      $rarity = 'r';
    }
    else if ($rarity == 'mythic rare' || $rarity == 'm') {
      $rarity = 'm';
    }

    $query .= "rarity='".$rarity."'";
    $lastSet = True;
  }

  echo "\nQUERY: " . $query;

  // Connect to the Database
  pg_connect('dbname=cs564_f12 host=postgres.cs.wisc.edu') 
	or die ("Couldn't Connect ".pg_last_error()); 

  // Execute the query and check for errors
  $result = pg_query($query);
  if (!$result) {
    $errormessage = pg_last_error();
    echo "Error with query: " . $errormessage;
    exit();
  }
  
  
  // get each row and print it out  
  while($row = pg_fetch_array($result,NULL,PGSQL_ASSOC))  {
    echo "        <tr>";
    echo "\n         <td align=\"center\">";
    echo "\n          ".$row['name'];
    echo "\n         </td>";
    echo "\n         <td align=\"center\">";

    // Set more readable name
    if ($row['type'] == 'c') {
      //echo "\n         <td align=\"center\" bgcolor=\"#cc0ccc\">";
      echo "\n          Creature";
    }
    else if ($row['type'] == 'l') {

      echo "\n          Land";
    }
    else if ($row['type'] == 's') {
    //echo "\n         <td align=\"center\">";
      echo "\n          Spell";
    }

    // Formatting things
    echo "\n         </td>";
    //    echo "\n         <td align=\"center\">";

    // Set more readable name
    if ($row['rarity'] == 'c') {
      echo "\n         <td align=\"center\">";
      echo "\n          Common";
    }
    else if ($row['rarity'] == 'u') {
      echo "\n         <td align=\"center\" bgcolor=\"#A4A4A4\">";
      echo "\n          Uncommon";
    }
    else if ($row['rarity'] == 'r') {
      echo "\n         <td align=\"center\" bgcolor=\"#FFBF00\">";
      echo "\n          Rare";
    }
    else if ($row['rarity'] == 'm') {
      echo "\n         <td align=\"center\" bgcolor=\"#DF3A01\">";
      echo "\n          Mythic Rare";
    }

    // Formatting things
    echo "\n         </td>";
    echo "\n        </tr>";
  }
  pg_close();
?>
 </table>
     </td>
    </tr>
        <p>
        <?php echo "<a href=\"https://cs564.cs.wisc.edu/laska/advanced_search.html\">Back to advance search</a>\n"?><p>

        <?php echo "<a href=\"https://cs564.cs.wisc.edu/laska/index.html\">Back to main page</a>\n"?>
 </body>

</html>
